﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;

public partial class school_class_room : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count == 0)
            {
                lblMessage.Text = "Please set up a school profile firstly!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }
        }
    }

    //private void LoadSchoolList()
    //{
    //    ddlSchoolList.Items.Clear();
    //    SchoolProfileTableList _profileList = SchoolProfileTableManager.GetList();
    //    if (_profileList != null)
    //    {
    //        foreach (SchoolProfileTable _profile in _profileList)
    //        {
    //            ddlSchoolList.Items.Add(new ListItem(_profile.SchoolName, _profile.SchoolKey.ToString()));
    //        }
    //    }
    //}

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlLocation.DataBind();
        ClearFields();
    }

   
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            ClassRoomTable _ClassRoom = new ClassRoomTable();
            _ClassRoom.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
            _ClassRoom.LocationKey = Convert.ToInt32(ddlLocation.Text);
            _ClassRoom.ClassRoomKey = Convert.ToInt32(lblClassRoomKey.Text);
            _ClassRoom.ClassRoomCode = txtClassRoomCode.Text;
            _ClassRoom.ClassRoomName = txtClassRoomName.Text;
            _ClassRoom.RoomNo = txtRoomNo.Text;
            _ClassRoom.UnitNo = txtUnitNo.Text;
            _ClassRoom.LevelNo = txtLevelNo.Text;
            _ClassRoom.SeatNo = Convert.ToInt16(txtNoOfSeats.Text);
            _ClassRoom.Remark = Server.HtmlEncode(txtRemark.Text);
            _ClassRoom.Status = 1;

            if (ClassRoomTableManager.Save(_ClassRoom) > 0)
            {
                ClearFields();
                grvClassRoom.DataBind();
                lblMessage.Text = "Saved sussccessfully!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Saving failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblClassRoomKey.Text.Length > 0)
        {
            if (ClassRoomTableManager.Delete(Convert.ToInt32(lblClassRoomKey.Text)))
            {
                ClearFields();
                grvClassRoom.DataBind();
                lblMessage.Text = "Item was deleted!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Deleting failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMessage.Text = "Please select an item to delete.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void grvClassRoom_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        if (grvClassRoom.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvClassRoom.SelectedDataKey;
            int _ClassRoomKey = Convert.ToInt32(_selectedDataKey[0]);
            int _schoolKey = Convert.ToInt32(_selectedDataKey[1]);
            int _LocationKey = Convert.ToInt32(_selectedDataKey[2]);

            if (_LocationKey > 0 && _schoolKey > 0 && _ClassRoomKey > 0)
            {
                
                ClassRoomTable _classroom = ClassRoomTableManager.GetItem(_ClassRoomKey);
                if (_classroom != null)
                {
                    lblSchoolKey.Text = _classroom.SchoolKey.ToString();
                    lblLocationKey.Text = _classroom.LocationKey.ToString();
                    lblClassRoomKey.Text = _classroom.ClassRoomKey.ToString();
                    txtClassRoomCode.Text = _classroom.ClassRoomCode;
                    txtClassRoomName.Text = _classroom.ClassRoomName;
                    txtRoomNo.Text = _classroom.RoomNo;
                    txtUnitNo.Text = _classroom.UnitNo;
                    txtLevelNo.Text = _classroom.LevelNo;
                    txtNoOfSeats.Text = _classroom.SeatNo.ToString();
                    txtRemark.Text = _classroom.Remark;
                }
                //GridViewRow _selectedRow = grvClassRoom.SelectedRow;
                //txtClassRoomCode.Text = _selectedRow.Cells[3].Text;
                //txtClassRoomName.Text = _selectedRow.Cells[4].Text;
                //txtRoomNo.Text = _selectedRow.Cells[5].Text;
                //lblMessage.Text = _selectedRow.Cells[4].Text;
                //CheckBox _chkStatus = (CheckBox)(_selectedRow.Cells[6].Controls[0]);
                //chkStatus.Checked = _chkStatus.Checked;
                //lblMessage.Text = _chkStatus.Checked.ToString();
            }
        }
    }

    private void ClearFields()
    {
        lblMessage.Text = "";
        lblSchoolKey.Text = ddlSchoolList.Text;
        lblLocationKey.Text = ddlLocation.Text;
        lblClassRoomKey.Text = "-1";
        txtClassRoomCode.Text = "";
        txtClassRoomName.Text = "";
        txtRoomNo.Text = "";
        txtUnitNo.Text = "";
        txtLevelNo.Text = "";
        txtNoOfSeats.Text = "";
        txtRemark.Text = "";
        //chkStatus.Checked = false;
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnShowClassRoom_Click(object sender, EventArgs e)
    {

    }
}
